//
//  changePayPassWordViewController.swift
//  AreaChainblock
//
//  Created by 赵佟越 on 2018/10/10.
//  Copyright © 2018年 zhaotongyue. All rights reserved.
//

import UIKit
import Foundation
import TKSubmitTransition
import QuartzCore


class changePayPassWordViewController: BaseViewController, UIViewControllerTransitioningDelegate {
    var timebutton: TimerButton!
    var btn: TKTransitionSubmitButton!
    var phoneTextField :UITextField!
    var codeTextField :UITextField!
    
    var btnFromNib: TKTransitionSubmitButton!
    
    override var preferredStatusBarStyle: UIStatusBarStyle {
        return .lightContent
    }
    override func viewDidLoad() {
        super.viewDidLoad()
        layoutNav()
        makeBaseUI()
    }
    //MARK: - PrivateMethods
    func layoutNav(){
        self.title = XALanguageManager.getString(key: "修改支付密码")
    }
    func makeBaseUI(){
        let bg = UIImageView(image: UIImage(named: ""))
        bg.backgroundColor = UIColor.clear
        bg.frame = self.view.frame
        self.view.addSubview(bg)
        
        
        let backWhiteView = UIView(frame: CGRect(x: 15, y: 80, width: kScreenWidth - 30, height: 88))
        
        backWhiteView.backgroundColor = UIColor.clear
        backWhiteView.layer.cornerRadius = 5
        backWhiteView.clipsToBounds = true
        self.view.addSubview(backWhiteView)
        
        let LineView = UIView(frame: CGRect(x: 5, y: 43.5, width: kScreenWidth - 40, height: 1))
        LineView.backgroundColor = UIColor("#EEEEEE")
        backWhiteView.addSubview(LineView)
        
        let LineView1 = UIView(frame: CGRect(x: 5, y: 43.5 + 43.5, width: kScreenWidth - 40, height: 1))
        LineView1.backgroundColor = UIColor("#EEEEEE")
        backWhiteView.addSubview(LineView1)
        
        let phoneLabel = UILabel(frame: CGRect(x: 15, y: 0, width: 100, height: 43))
        phoneLabel.text = XALanguageManager.getString(key: "Phone")
       phoneLabel.textColor = UIColor.white
        backWhiteView.addSubview(phoneLabel)
        
        
        
        
        
        phoneTextField = ZTYTextField(leftView: nil, frame: CGRect(x:  100 , y: 0, width: kScreenWidth - 130, height: 43), rightView: nil, color: UIColor.black, boardWidth: 0.8, boardRadius: 4)
        backWhiteView.addSubview(phoneTextField)
        phoneTextField.textColor = UIColor.white
        //        phoneTextField.keyboardType = .numberPad
        let placeholserAttributes = [NSAttributedStringKey.foregroundColor : UIColor("909090"),NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16)]
        phoneTextField.font = UIFont.systemFont(ofSize: 16)
        phoneTextField.keyboardType = .numberPad
        phoneTextField.attributedPlaceholder = NSAttributedString(string: XALanguageManager.getString(key: "请输入手机号"),attributes: placeholserAttributes)
        //        phoneTextField.borderStyle=UITextBorderStyle.roundedRect
                phoneTextField.layer.borderColor = UIColor.clear.cgColor
        
        codeTextField = ZTYTextField(leftView: nil, frame: CGRect(x: 10 , y: 45, width: kScreenWidth - 130, height: 43), rightView: nil, color: UIColor.black, boardWidth: 0.8, boardRadius: 4)
        backWhiteView.addSubview(codeTextField)
        codeTextField.isSecureTextEntry = true
        codeTextField.textColor = UIColor.white
        
        let placeholserAttributes1 = [NSAttributedStringKey.foregroundColor : UIColor("909090"),NSAttributedStringKey.font : UIFont.systemFont(ofSize: 16)]
        codeTextField.font = UIFont.systemFont(ofSize: 16)
        codeTextField.attributedPlaceholder = NSAttributedString(string: XALanguageManager.getString(key:"请输入验证码"),attributes: placeholserAttributes1)
        
        timebutton = TimerButton(frame: CGRect(x: kScreenWidth - 110 , y: 45 + 4.5, width: 70, height: 35))
        timebutton.backgroundColor = UIColor("#297EFB")
        timebutton.layer.cornerRadius = 5
        timebutton.setup(XALanguageManager.getString(key: "发送"), timeTitlePrefix: "", aTimeLength: 60)
        
        backWhiteView.addSubview(timebutton)
        timebutton.clickBtnEvent = {
            () -> Void in
            
            if !self.timebutton.isWorking{
                getData()
                //访问接口，获取验证码
            }
        }
        func getData() {
            //
            MyNetwork.request(target: .sendcode(mobile: phoneTextField.text!, token: String().md5(inString: "w2ZP4b~y1xv9A!ir38&Z"), type: "3"), success: { (result) in
                kLog(result)
                let needModel = BaseModel()
                needModel.yy_modelSet(with: result as! [AnyHashable : Any])
                
                if needModel.code == 1111{
                    self.timebutton.isWorking = true
                    self.view.makeToast(XALanguageManager.getString(key: XALanguageManager.getString(key: "成功")))
                }else{
                    self.timebutton.setup(XALanguageManager.getString(key: "发送"), timeTitlePrefix: "", aTimeLength: 60)
                    kLog(needModel.message)
                    self.view.makeToast(XALanguageManager.getString(key: XALanguageManager.getString(key: "失败")))
                }
                
            }) { (error) in
                self.timebutton.setup(XALanguageManager.getString(key: "发送"), timeTitlePrefix: "", aTimeLength: 60); self.view.makeToast(error.localizedDescription)
                //                kLog(error.localizedDescription)
            }
        }
        //        timebutton.addTarget(self, action: #selector(ZhuCeViewController.sendAction), for: .touchUpInside)
        
        
        //        phoneTextField.borderStyle=UITextBorderStyle.roundedRect
                codeTextField.layer.borderColor = UIColor.clear.cgColor
        
        //
        //        let zhuceBtn = UIButton(type: .system)
        //        zhuceBtn.setTitle(XALanguageManager.getString(key: "注册"), for: .normal)
        //        self.view.addSubview(zhuceBtn)
        //        zhuceBtn.setTitleColor(UIColor.black, for: .normal)
        //        zhuceBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //        zhuceBtn.contentHorizontalAlignment = .left
        //        zhuceBtn.snp.makeConstraints { (make) in
        //            make.top.equalTo(codeTextField.snp.bottom).offset(20)
        //            make.left.equalTo(self.view).offset(15)
        //            make.size.equalTo(CGSize(width: 120, height: 15))
        //        }
        //
        //
        //        zhuceBtn.addTarget(self, action: #selector(LogInViewController.backAction), for: .touchUpInside)
        
        
        //        let passwordForgetBtn = UIButton(type: .system)
        //        passwordForgetBtn.contentHorizontalAlignment = .right
        //        passwordForgetBtn.setTitle(XALanguageManager.getString(key: "忘记密码"), for: .normal)
        //        self.view.addSubview(passwordForgetBtn)
        //        passwordForgetBtn.setTitleColor(UIColor.black, for: .normal)
        //        passwordForgetBtn.titleLabel?.font = UIFont.systemFont(ofSize: 15)
        //        passwordForgetBtn.snp.makeConstraints { (make) in
        //            make.top.equalTo(codeTextField.snp.bottom).offset(20)
        //            make.right.equalTo(self.view).offset(-15)
        //            make.size.equalTo(CGSize(width: 120, height: 15))
        //        }
        //        passwordForgetBtn.addTarget(self, action: #selector(LogInViewController.backAction), for: .touchUpInside)
        //
        
        
        btn = TKTransitionSubmitButton(frame: CGRect(x: 15, y: 200, width: kScreenWidth - 30, height: 44))
        btn.layer.cornerRadius = 4;
        btn.backgroundColor = UIColor("#297EFB")
        //        btn.frame.bottom = self.view.frame.height - 60
        
        btn.setTitle(XALanguageManager.getString(key: "下一步"), for: UIControlState())
        btn.titleLabel?.font = UIFont(name: "HelveticaNeue-Light", size: 14)
        self.view.addSubview(btn)
        btnFromNib = TKTransitionSubmitButton()
        btn.addTarget(self, action: #selector(changePayPassWordViewController.nextAction), for: UIControlEvents.touchUpInside)
    }
    
    @objc func backAction(){
        
        self.dismiss(animated: true) {
            
        }
    }
    @objc func  sendAction() {
        
    }
    @objc func nextAction()  {
        //        let conVC
        //            = changePassWordDetailViewController()
        //        //                conVC.phoneNumber = self.phoneTextField.text! as NSString;
        //        self.navigationController?.pushViewController(conVC, animated: true)
        
     
        
        
        MyNetwork.request(target: .checkcode(mobile: phoneTextField.text!, type: "3", code: codeTextField.text!), success: { (result) in
            kLog(result)
            let needModel = BaseModel()
            needModel.yy_modelSet(with: result as! [AnyHashable : Any])
            
            if needModel.code == 1111{
                let conVC
                    = changePayPasswordDetailViewController()
                //                conVC.phoneNumber = self.phoneTextField.text! as NSString;
                self.navigationController?.pushViewController(conVC, animated: true)
                //                 self.view.makeToast(needModel.message)
            }else{
                self.navigationController?.view.makeToast(XALanguageManager.getString(key: "失败"))
            }
            
        }) { (error) in
            self.navigationController?.view.makeToast(error.localizedDescription)
            kLog(error.localizedDescription)
        }
        
        
        
    }
    @objc func onTapButton(_ button: TKTransitionSubmitButton) {
        
        button.animate(0.7, CAMediaTimingFunction(name: kCAMediaTimingFunctionEaseInEaseOut), completion: { () -> () in
            
            self.dismiss(animated: false, completion: {
                
            })
        })
        
        self.view.bringSubview(toFront: self.btnFromNib)
    }
    
    // MARK: UIViewControllerTransitioningDelegate
    func animationController(forPresented presented: UIViewController, presenting: UIViewController, source: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return TKFadeInAnimator(transitionDuration: 0.5, startingAlpha: 0.8)
    }
    
    func animationController(forDismissed dismissed: UIViewController) -> UIViewControllerAnimatedTransitioning? {
        return nil
    }
}

